package com.platform.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.EnergyCoinEntity;
import com.platform.service.EnergyCoinService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 能量币记录表Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-05-22 13:50:37
 */
@RestController
@RequestMapping("energycoin")
public class EnergyCoinController {
    @Autowired
    private EnergyCoinService energyCoinService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("energycoin:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        System.out.println(JSONObject.toJSONString(params));

        List<EnergyCoinEntity> energyCoinList = energyCoinService.queryList(query);
        int total = energyCoinService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(energyCoinList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("energycoin:info")
    public R info(@PathVariable("id") Long id) {
        EnergyCoinEntity energyCoin = energyCoinService.queryObject(id);

        return R.ok().put("energyCoin", energyCoin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("energycoin:save")
    public R save(@RequestBody EnergyCoinEntity energyCoin) {
        energyCoinService.save(energyCoin);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("energycoin:update")
    public R update(@RequestBody EnergyCoinEntity energyCoin) {
        energyCoinService.update(energyCoin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("energycoin:delete")
    public R delete(@RequestBody Long[] ids) {
        energyCoinService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<EnergyCoinEntity> list = energyCoinService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("queryAllCoin")
    public R queryAllCoin(@RequestParam Map<String,Object> param ){
        if(param.get("name")==""&&param.get("type")==""&&param.get("incomeType")==""){
            return R.ok().put("coins",0);
        }
        return R.ok().put("coins",energyCoinService.queryAllCoin(param));
    }
}
